﻿using System;
using System.Web.UI.WebControls;
using Alserve.Ffl;
using Alserve.Ffl.Data;

public partial class ffl_user_webusercontrols_playerpicker : System.Web.UI.UserControl
{
    //private Int32 _goalkeeperCount;
    //private Int32 _defenderCount;
    //private Int32 _midfielderCount;
    //private Int32 _forwardCount;

    public event EventHandler ControlRefreshed;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            UpdatePositionCounts();
            UpdateClubDataSource();
            UpdatePlayerClubDataSource();
        }
    }

    public void UpdateClubDataSource()
    {
        Club thisClub = new Club();
        String clubNotInList = String.Empty;
        
        _UserPlayerSquad thisSquad = new _UserPlayerSquad();
        thisSquad.Where.UserId.Value = Convert.ToInt32(Session["UserId"]);

        thisSquad.Query.AddResultColumn(_UserPlayerSquad.ColumnNames.ClubId);
        thisSquad.Query.AddResultColumn(_UserPlayerSquad.ColumnNames.ClubName);
        if (thisSquad.Query.Load())
        {
            while (!thisSquad.EOF)
            {
                clubNotInList += thisSquad.ClubId.ToString() + ",";
                thisSquad.MoveNext();
            }
        }

        if (clubNotInList.Length >= 1)
        {
            thisClub.Where.Id.Value = clubNotInList.Substring(0, clubNotInList.Length - 1);
            thisClub.Where.Id.Operator = MyGeneration.dOOdads.WhereParameter.Operand.NotIn;
        }

        if (thisClub.Query.Load())
        {
            thisClub.Sort = "ClubName";
            ClubDropDown.DataSource = thisClub.DefaultView;
            ClubDropDown.DataBind();
        }
        
    }

    protected void ClubDropDown_SelectedIndexChanged(object source, EventArgs e)
    {
        UpdatePlayerClubDataSource();
    }

    protected void PositionDropDown_SelectedIndexChanged(object source, EventArgs e)
    {
        UpdatePlayerClubDataSource();
    }

    public void UpdatePlayerClubDataSource()
    {
        if (ClubDropDown.SelectedItem != null && PositionDropDown.SelectedItem != null)
        {
            _PlayerClubView thisPcv = new _PlayerClubView();
            thisPcv.Where.ClubId.Value = Convert.ToInt32(ClubDropDown.SelectedItem.Value);
            thisPcv.Where.PositionId.Value = Convert.ToInt32(PositionDropDown.SelectedItem.Value);

            PlayerPickerRepeater.DataSource = thisPcv.Query.Load() ? thisPcv.DefaultView : null;
            PlayerPickerRepeater.DataBind();
        }
    }


    protected void PlayerPickerRepeater_ItemDataBound(Object Sender, RepeaterItemEventArgs e)
    {
        
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            Image playerImage = (Image)e.Item.FindControl("PlayerImage");
            Int32 espnRef = Convert.ToInt32(((HiddenField)e.Item.FindControl("EspnRefHidden")).Value);
            Int32 clubId = Convert.ToInt32(ClubDropDown.SelectedValue);
            playerImage.ImageUrl = FflCommon.ImageLocation(espnRef, clubId);
        }
    }

    protected void PlayerPickerRepeater_ItemCommand(object source, RepeaterCommandEventArgs e)
    {

        Button addPlayerButton = (Button)e.Item.FindControl("AddPlayerButton");
        addPlayerButton.Enabled = false;

        Int32 userId = Convert.ToInt32(Session["UserId"]);
        Int32 playerId = Convert.ToInt32(((HiddenField)e.Item.FindControl("PlayerIdHidden")).Value);

        UserPlayer thisUserPlayer = new UserPlayer();
        thisUserPlayer.AddNew();
        thisUserPlayer.UserId = userId;
        thisUserPlayer.PlayerId = playerId;
        thisUserPlayer.Save();

        UpdatePositionCounts();
        UpdateClubDataSource();
        UpdatePlayerClubDataSource();
        if (this.ControlRefreshed != null)
        {
            this.ControlRefreshed(this, new EventArgs());
        }
    }

    public void UpdatePositionCounts()
    {
        Int32[] positionCount = new Int32[5];

        UserPlayerScores thisUserPlayer = new UserPlayerScores();
        thisUserPlayer.Where.UserId.Value = Convert.ToInt32(Session["UserId"]);

        if (thisUserPlayer.Query.Load())
        {
            while (!thisUserPlayer.EOF)
            {
                positionCount[thisUserPlayer.PositionId]++;
                thisUserPlayer.MoveNext();
            }
        }

        Position thisPosition = new Position();
        String positionInList = String.Empty;
        if (positionCount[1] < 2)
            positionInList += "1,";
        if (positionCount[2] < 7)
            positionInList += "2,";
        if (positionCount[3] < 6)
            positionInList += "3,";
        if (positionCount[4] < 5)
            positionInList += "4,";

        if (positionInList.Length > 0)
        {
            positionInList = positionInList.Substring(0, positionInList.Length - 1);
            thisPosition.Where.Id.Value = positionInList;
            thisPosition.Where.Id.Operator = MyGeneration.dOOdads.WhereParameter.Operand.In;

            if (thisPosition.Query.Load())
            {
                PositionDropDown.DataSource = thisPosition.DefaultView;
                PositionDropDown.DataBind();
            }
        }

        PlayerPickerPanel.Visible = thisPosition.RowCount !=0;
        PositionDropDown.DataBind();
    }

    
    
}
